# GNAFU - "Ground, Navy and Air Fuck-Up" real-time strategy game
# Copyright (C) 2008-2009 k0wax <k0waxk0wax@gmail.com>
#
# This file is part of GNAFU.
#
# GNAFU is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GNAFU is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNAFU.  If not, see <http://www.gnu.org/licenses/>.

"""Various loading routines"""
from __future__ import division, absolute_import
import os, sys
import pyglet
import yaml
from src import vars, conf, res

def _load_props(*args):
    return load_props_cls
    obj = args[0]
    if type(obj) is type:
        return load_props_cls
    else:
        return load_props_obj
        

def load_props(*args):
    def decorate(cls):
        _dir = conf.data_path
        s = args[0]
        if '/' in s:
            _file = s.rstrip('/').split('/')[-1]
            _dir = os.path.join(_dir, s.rstrip('/').rstrip(_file))
            if s[-1:] == '/':
                #print _dir, _file
                _dir = os.path.join(_dir, _file)
        else:
            _file = s
        file = os.path.join(_dir, _file + '.cfg')
        if not os.path.exists(file):
            print 'WARN WARN WARN config %s not found' % file
            #print _dir, _file
            raise RuntimeError
        d = yaml.safe_load(open(file))
        d_object = d['object']
        object_name = d_object.get('name', os.path.splitext(os.path.basename(file))[0])
        d_object['classname'] = object_name
        print 'CLASSNAME', object_name
        # set class props
        for k, v in d_object.items():
            setattr(cls, k, v)
        print cls.classname
        # load resources
        #if 'gfx.enable' in conf.vars and conf.vars['gfx.enable'] == True \
        #    and 'images' in d_object: # FIXME XXX
        if 'images' in d_object:
            # load images
            for id, p in d_object['images'].items():
                rows = p['rows'] if 'rows' in p else 1
                frames = p['frames'] if 'frames' in p else 1
                #name = 'img' if id == 'default' else 'img_%s' % id
                name = object_name + '_' + id
                img = pyglet.image.load(os.path.join(_dir, p['file']))
                if frames > 1:
                    img_grid = pyglet.image.ImageGrid(img, rows, frames)
                    img_grid.anchor_y = (img_grid.height // 2) - (vars.tile_size // 2)
                    img_grid.anchor_x = ((img_grid.width // frames) // 2) - (vars.tile_size // 2)
                    res.add_image(name, img_grid)
                    res.add_image(name + '_src', img)
                else:
                    res.add_image(name, img)
        return cls
    return decorate

